package com.jiyun.user.controller;

import com.jiyun.user.OSS.AliyunOSSUtils;
import com.jiyun.Result.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;

@RestController
@Slf4j
public class FileUploadController {

    @Autowired
    private AliyunOSSUtils aliyunOSSUtils;

    @PostMapping("/upload/fileUpload")
    public Result upload(MultipartFile file) throws IOException {

//        获取文件名
        String filename = file.getOriginalFilename();
//        获取Io流
        InputStream inputStream = file.getInputStream();
        //文件上传
        String url = aliyunOSSUtils.putObject(inputStream, filename);
        log.info("文件上传成功，文件地址为：{}", url);
        return Result.ok(url);
    }
}
